package com.reds.backend.controller.photoController;

import com.reds.backend.utils.adminUtils.admin.StpAdminUtil;
import com.reds.backend.utils.photoUtils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class photoUpdateController {

    @Autowired
    private QiniuUtils qiniuUtils;

    @PostMapping("/photo/")
    public Map<String,String> upload(@RequestParam("file") MultipartFile file){//上传文件，返回URL
        //StpAdminUtil.checkRoleOr("admin","superAdmin");
        String originalFilename = file.getOriginalFilename();
        //唯一的文件名称 使用UUID
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        System.out.println(file);
        //上传文件到七牛云
        boolean upload = qiniuUtils.upload(file, newName);
        Map<String,String> map = new HashMap<>();
        if(upload){
            System.out.println(QiniuUtils.url  +newName);

            map.put("location",QiniuUtils.url+newName);
            System.out.println(map);
            return map;
        }
        map.put("error","error");
        return map;
    }
    @PostMapping("/face/")
    public String uploadFace(@RequestParam("image") MultipartFile file){
        StpAdminUtil.checkRoleOr("admin","superAdmin");
        String originalFilename = file.getOriginalFilename();
        //唯一的文件名称 使用UUID
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        System.out.println(file);
        //上传文件到七牛云
        boolean upload = qiniuUtils.upload(file, newName);
        if(upload){
            System.out.println(QiniuUtils.url  +newName);
            return QiniuUtils.url  +newName;
        }
        return "error";
    }
}